****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
*By Andrew Bibler & Stephen B Billings

****************************************************************************
clear all 
set more off

use CMS_Lot_Choices.dta, clear

keep year dl litd mont ib lead arts trad accel open magnet choicenumber school 

gen appch1_link = choicenumber 
gen appch1schl = school 
foreach x in dl litd mont ib lead arts trad accel open magnet school { 
rename `x' appch1`x'   
	} 
tempfile app1choices
		save `app1choices'

rename appch1_link assch_link 
rename appch1schl assschl 
foreach x in dl litd mont ib lead arts trad accel open magnet school { 
rename appch1`x' assch`x' 
	} 
tempfile asschoices
		save `asschoices'
	
use "CMS_Clean_Long.dta" , clear
				
merge m:1 appch1_link year using `app1choices' , gen(app1merge)

keep if (app1merge==1 | app1merge==3)

merge m:1 mastid using frl_mb3.dta , gen(frl_mb3merge)    

keep if (frl_mb3merge==1 | frl_mb3merge==3)    
      
gen testgrade = grade  
keep if grade!=.

gen mbyear = year 
 
merge 1:1 mastid testgrade mbyear using nc_chars.dta , gen(mbmerge)    

keep if (mbmerge==1 | mbmerge==3)  

gen exit = (mbmerge==1)  
 
xtset mastid grade 

gen home_lag1 = L.homeschl
gen home_lag1_miss = (home_lag1==.) 


gen home_lead1 = F.homeschl 
gen home_lead1_miss = (home_lead1==.) 

gen curr_lead1 = F.currschool
gen curr_lead1_miss = (curr_lead1==.) 

gen curr_lag1 = L.currschool
gen curr_lag_miss = (curr_lag1==.)

replace sciencestand=0 if year<2008
replace sci_grlevel=1 if year<2008

gen math_lag = L.mathstand 
gen math_lag2 = L2.mathstand

gen read_lag = L.readstand 
gen read_lag2 = L2.readstand	

gen science_lag = L.sciencestand 
gen science_lag2 = L2.sciencestand

gen frlmb_lag = L.frl_mb
gen frlmb_lag2 = L2.frl_mb

gen lep_Y_lag = L.lep_Y
gen lep_Y_lag2 = L2.lep_Y

gen math_gr_lag = L.math_grlevel
gen math_gr_lag2 = L2.math_grlevel	

gen read_gr_lag = L.read_grlevel
gen read_gr_lag2 = L2.read_grlevel

gen science_gr_lag = L.sci_grlevel
gen science_gr_lag2 = L2.sci_grlevel

gen mardgr_int_lag = math_gr_lag*read_gr_lag
gen mardgr_int_lag2 = math_gr_lag2*read_gr_lag2

gen allgr_int_lag = math_gr_lag*read_gr_lag*science_gr_lag
gen allgr_int_lag2 = math_gr_lag2*read_gr_lag2*science_gr_lag2

gen ma_aig_lag = L.ma_aig
gen ma_aig_lag2 = L2.ma_aig

gen rd_aig_lag = L.rd_aig
gen rd_aig_lag2 = L2.rd_aig

gen aig_int_lag = rd_aig_lag*ma_aig_lag
gen aig_int_lag2 = rd_aig_lag2*ma_aig_lag2

gen app1 = (appch1!=0) 
gen app2 = (appch2!=0) 
gen app3 = (appch3!=0)

egen wonany = rowmax(wonfirst wonsecond wonthird)

foreach l in 1 {
	egen move`l' = diff(homeschl home_lead`l')
	egen attdiff`l' = diff(homeschl curr_lead`l')
	gen atthome`l' = (1 - attdiff`l')
	}

gen changehs = (move1==1 & home_lead1!=.)

su frpl if grade==0 
replace frpl = `r(mean)' if (grade==0 & year>2010)

su frpl if grade==6 
replace frpl = `r(mean)' if (grade==6 & year>2010)

foreach y in 07 08 09 10 11 12 13 {
	gen frpl`y' = frpl if year==20`y'
	replace frpl`y' = 0 if year!=20`y'
	}

gen lunch = frpl if year<=2010

replace lunch = frlmb_lag if (year>2010 & grade==6) 	

replace lunch = frl_mb3 if (year>2010 & grade==0) 

gen lunch_miss = (lunch==.)

foreach g in 0 6 {	
foreach y in 11 12 13 {
	su lunch if ( year==20`y' & grade==`g' )
	replace lunch=`r(mean)' if (lunch==. & year==20`y' & grade==`g' )
	}
	}
	
foreach y in 07 08 09 10 11 12 13 {
	gen lunch`y' = lunch if year==20`y'
	replace lunch`y' = 0 if year!=20`y'
	}

gen ksample = (grade==0 & year>2006)

gen sixsample = (grade==6 & cms!=. & charter!=. & exit!=. & math_lag!=. & read_lag!=. & science_lag!=. & lunch!=. & math_gr_lag!=. & read_gr_lag!=. & science_gr_lag!=. & ma_aig_lag!=. & rd_aig_lag!=.)

merge m:1 mastid using malefem.dta , gen(merge_sex) 
keep if (merge_sex==3) 
	
gen assch_link = asschoice

merge m:1 assch_link year using `asschoices' , gen(merge_assch)

keep if inlist(merge_assch , 1 , 3)

merge m:1 assschl using "scores-assigned.dta" , gen(merge_assscores)

keep if inlist(merge_assscores , 1 , 3)

merge m:1 curr_lead1 using "scores-attended.dta" , gen(merge_attscores)

keep if inlist(merge_attscores , 1 , 3)

keep if (year>2008 & year<2013)

keep if ( grade==0 | grade==6 )

gen attappch1schl = (curr_lead1==appch1school) & appch1school!=.

gen attassschl = (curr_lead1==assschl) 

gen nccharter = charter==1 if grade==6

save allcms-summary.dta , replace










